Skip to content

fix(storage): compare size by raw bytes instead of localized float - #714

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-371721-compare-storage-size-by-raw-bytes
Jul 28, 2026
Merged

fix(storage): compare size by raw bytes instead of localized float#714
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-371721-compare-storage-size-by-raw-bytes

Conversation

@add-uos

@add-uos add-uos commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Extract raw byte count before brackets for accurate comparison across locales with different thousand separators (comma/dot).

提取方括号前的原始字节数进行比较,解决不同locale下千分位
分隔符(逗号/点)导致的大小比较错误。

Log: 修复存储设备大小比较在非中文locale下出错的问题
PMS: 371721
Influence: 存储设备大小比较逻辑,影响smartctl解析的设备容量显示

Summary by Sourcery

Bug Fixes:

  • Correct storage device size comparison by parsing and comparing raw byte values to avoid errors under locales with different thousand separators.

Extract raw byte count before brackets for accurate comparison
across locales with different thousand separators (comma/dot).

提取方括号前的原始字节数进行比较,解决不同locale下千分位
分隔符(逗号/点)导致的大小比较错误。

Log: 修复存储设备大小比较在非中文locale下出错的问题
PMS: 371721
Influence: 存储设备大小比较逻辑,影响smartctl解析的设备容量显示
@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

DeviceStorage::compareSize now compares storage sizes using the raw byte count extracted from the localized smartctl output instead of parsing the human-readable value inside brackets, fixing locale-dependent comparison errors.

File-Level Changes

Change Details Files
Compare storage sizes using raw byte counts parsed before brackets, handling locale-specific thousand separators and byte suffixes.
  • Replaced previous float-based parsing of the value inside brackets with a lambda extractBytes that extracts the leading numeric byte count before '['
  • Updated the regular expression to capture a number with optional thousand separators (comma or dot) and an optional 'bytes' suffix before the opening bracket
  • Normalized the captured byte string by removing commas and dots and converting it to a quint64 via toULongLong
  • Changed comparison logic from epsilon-based float comparison on int values to a simple unsigned integer comparison of bytes1 and bytes2
  • Updated debug logging messages to reflect the new bytes-based comparison while retaining the existing else-branch log message text
deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The second qCDebug message still references num1/num2 even though the logic now uses bytes1/bytes2, which can confuse future readers and should be updated to match the new variables.
  • Consider handling space or non-breaking-space thousand separators in extractBytes (common in some locales) and/or documenting that only comma/dot-separated formats are supported, to avoid future surprises when new locales are introduced.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The second qCDebug message still references `num1`/`num2` even though the logic now uses `bytes1`/`bytes2`, which can confuse future readers and should be updated to match the new variables.
- Consider handling space or non-breaking-space thousand separators in `extractBytes` (common in some locales) and/or documenting that only comma/dot-separated formats are supported, to avoid future surprises when new locales are introduced.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@add-uos
add-uos force-pushed the fix-371721-compare-storage-size-by-raw-bytes branch from 0ca76cc to 24f35d0 Compare July 28, 2026 03:24
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码通过提取原始字节数优化了存储大小比较逻辑,解决了单位混淆和数值溢出问题。
逻辑严谨且类型安全,无安全漏洞,仅在正则表达式构造上有微小优化空间。

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

在 DeviceStorage::compareSize 函数中,使用正则表达式提取方括号前的原始字节数,移除千分位分隔符后转为 quint64 进行比较,避免了旧代码中 int 溢出和浮点数比较的精度问题。
潜在问题:如果输入字符串中不包含方括号或字节数,extractBytes 返回 0,可能导致两个无效输入比较时返回 size2,但这符合现有逻辑预期。
建议:保持当前逻辑,对异常输入的容错处理已足够。

  • 2.代码质量(良好)✓

使用 lambda 封装提取逻辑,减少了代码重复;注释清晰说明了不同语言环境下的格式差异;使用 quint64 替代 int 更符合字节大小的实际范围。
潜在问题:lambda 内部每次调用都会构造 QRegularExpression 对象,虽然影响极小,但不符合最优实践。
建议:将 QRegularExpression 声明为 static 局部变量,避免重复编译正则表达式。

  • 3.代码性能(无性能问题)✓

正则匹配和字符串处理在单次函数调用中执行两次,开销极低,不会对性能产生影响。
建议:无需额外优化。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
总体风险描述:代码仅处理字符串解析与数值比较,不涉及系统命令执行或外部不可信输入注入,无安全风险。

  • 建议:无需修复。

■ 【改进建议代码示例】

// 提取原始字节数(方括号前的数字,千分位逗号/点)比较
auto extractBytes = [](const QString &str) -> quint64 {
    static QRegularExpression reg("(\\d[\\d,.]*)\\s*(?:bytes\\s*)?\\[");
    QRegularExpressionMatch match = reg.match(str);
    if (match.hasMatch()) {
        QString raw = match.captured(1);
        raw.remove(",");
        raw.remove(".");
        return raw.toULongLong();
    }
    return 0;
};

quint64 bytes1 = extractBytes(size1);
quint64 bytes2 = extractBytes(size2);

if (bytes1 > bytes2) {
    qCDebug(appLog) << "DeviceStorage::compareSize, bytes1 > bytes2";
    return size1;
} else {
    qCDebug(appLog) << "DeviceStorage::compareSize, bytes1 <= bytes2";
    return size2;
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 18b5021 into linuxdeepin:master Jul 28, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants